package com.polytech.diploma.OracleStat;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.exception.KettleDatabaseException;

public class OracleTablesStat {
	
	private static OracleTablesStat tablesStat = null;
	private Map<String, TableStatData> tables = null;
	private Database DBConnection;
	
	private OracleTablesStat (Database DBConnection)
	{
		this.tables = new HashMap<String, TableStatData> ();	
		this.DBConnection = DBConnection;
	}
	
	public static OracleTablesStat getInstance(String schemaName, Database DBConnection)
	{
		if(tablesStat == null)
		{
			tablesStat = new OracleTablesStat(DBConnection);
			tablesStat.fillData(schemaName);
		}
		
		return tablesStat;
	}
	
	private void fillData(String schemaName)
	{
		/*
		 * Connection.getTableList(schemaName);
		 * fill tables with data (key - TableName; value - new TableStatData)
		 */
		String query = 	"SELECT owner, table_name " +
						"FROM dba_tables " +
						"WHERE owner='" + schemaName + "'";
		try {
			this.DBConnection.connect();
			PreparedStatement pstmt = this.DBConnection.prepareSQL(query);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
			    tables.put(rs.getString("table_name"), 
			    		new TableStatData());
			}
			this.DBConnection.disconnect();
		} catch (KettleDatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	public void addTable(String tableName, CommandType cmdType)
	{
		switch(cmdType)
		{	
			case INSERT:
			{
				addTableInsert(tableName);
				break;
			}
			case DELETE:
			{
				addTableDelete(tableName);
				break;
			}
			case UPDATE:
			{
				addTableUpdate(tableName);
				break;
			}
			/*
			 * TODO: MERGE
			 */
		}
	}
	
	private void addTableInsert(String table)
	{
		TableStatData statData = tables.get(table.toUpperCase());
		if (statData != null)
		{
			statData.addInsert();
		}
	}
	
	private void addTableUpdate(String table)
	{
		TableStatData statData = tables.get(table.toUpperCase());
		if (statData != null)
		{
			statData.addUpdate();
		}
	}
	
	private void addTableDelete(String table)
	{
		TableStatData statData = tables.get(table.toUpperCase());
		if (statData != null)
		{
			statData.addDelete();
		}
	}
	
	public int getTableStat(String tableName)
	{
		TableStatData statData = tables.get(tableName.toUpperCase());
		if (statData != null)
		{
			return statData.getDeleteNum() + 
						statData.getInsertNum() + 
						statData.getUpdateNum();
		}
		return 0;
	}
	
	public int getUpdateTableStat(String tableName)
	{
		TableStatData statData = tables.get(tableName.toUpperCase());
		if (statData != null)
		{
			return statData.getUpdateNum();
		}
		return 0;
	}
	
	public int getDeleteTableStat(String tableName)
	{
		TableStatData statData = tables.get(tableName.toUpperCase());
		if (statData != null)
		{
			return statData.getDeleteNum();
		}
		return 0;
	}
	
	public int getInsertTableStat(String tableName)
	{
		TableStatData statData = tables.get(tableName.toUpperCase());
		if (statData != null)
		{
			return statData.getInsertNum();
		}
		return 0;
	}
}
